clear all
*cap log close
set more off
set seed 603

* main text figure: officer bunching -----------------------------------
* (overall and by officer type)

* store aggregate distribution ------------
use "${data}/out/4-main", clear
keep officerid
duplicates drop
merge 1:m officerid using "${data}/out/2-stopsfhp", keep(3) nogen
keep if viol_code==575 & speedd>=0 & speedd<=29

gen N = 1
collapse (mean) fine (sum) N, by(speeddiff)
egen Ntot=sum(N)
gen frac = N/Ntot
replace fine=0 if speedd<6
replace frac=0 if speedd<6

gen lenientpart = 2
sort speedd
tempfile aggregate
save    `aggregate'

* distribution by officer type ---------------
use "${data}/out/4-main", clear
keep officerid
duplicates drop
merge 1:m officerid using "${data}/out/2-stopsfhp", keep(3) nogen
keep if viol_code==575 & speedd>=0 & speedd<=29

gen N = 1
collapse (mean) fine (sum) N, by(speeddiff lenientpart)
bysort lenientpart: egen Ntot=sum(N)
gen frac = N/Ntot
replace fine=0 if speedd<6
replace frac=0 if speedd<6

sort lenientpart speedd
append using `aggregate'

* build plot ---------------------------------
#delimit ;
twoway connected frac speedd if lenientpart==1, 
	   mcolor(emidblue) lcolor(emidblue) msymbol(Dh) msize(medsmall)  lpattern(shortdash) ||
	   connected frac speedd if lenientpart==0,
	   mcolor(teal) lcolor(teal) msymbol(Th) msize(medsmall)  lpattern(shortdash) ||
	   connected frac speedd if lenientpart==2, 
	   mcolor(navy) lcolor(navy) msymbol(O) msize(medsmall) ||
	   line fine speedd if lenientpart==1, yaxis(2) lpattern(dash) lcolor(cranberry)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
ylab(0(0.1)0.5, nogrid) ylab(0(100)400, axis(2)) xlab(0(5)30, nogrid)
ytitle("Fraction of Citations") ytitle("Fine Amount", axis(2))
legend(ring(0) pos(2) cols(1) order(1 2) region(lstyle(border))
lab(1 "Lenient Officers") lab(2 "Stringent Officers") ) ;
#delimit cr
graph export "${out}/main/bunch.pdf", replace
* ------------------------------------------------------------------------



* appendix: bunching by radar gun status ---------------------------------
use "${data}/out/4-main", clear
keep officerid 
duplicates drop 
merge 1:m officerid using "${data}/out/2-stopsfhp", keep(3) nogen 
keep if viol_code==575 & !mi(speeddiff) & (speeddiff>=0) & (speeddiff<=29)

* store overall histogram -----------
preserve 
gen N = 1 
collapse (sum) N (mean) fine, by(speeddiff) fast 
egen Ntotal = sum(N)
gen frac = N/Ntotal 
replace fine=0 if speeddiff<6
gen radar = 2 
tempfile add 
save    `add'
restore 

* by radar gun status -------------------
gen radar = (arrestmethod==3)
gen N = 1 
collapse (sum) N, by(radar speeddiff)
bysort radar: egen Ntotal = sum(N)
gen frac = N/Ntotal 
qui append using `add'

* build plot ---------------------------
#delimit ;
twoway connected frac speedd if radar==1, 
	   mcolor(navy) lcolor(emidblue) msymbol(D) msize(medsmall)  lpattern(shortdash) ||
	   connected frac speedd if radar==0,
	   mcolor(dkgreen) lcolor(teal) msymbol(T) msize(medsmall)  lpattern(shortdash) ||
	   line fine speedd if radar==2, yaxis(2) lpattern(dash) lcolor(maroon)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
ylab(0(0.1)0.5, nogrid) ylab(0(100)400, axis(2) nogrid) xlab(0(5)30, nogrid)
ytitle("Fraction of Citations") ytitle("Fine Amount", axis(2))
legend(ring(0) pos(2) cols(1) order(1 2)
lab(1 "Radar Citations") lab(2 "Other Citations") region(lstyle(border))) ;
#delimit cr
graph export "${out}/apx_iv/bunch_radar.pdf", replace
* ------------------------------------------------------------------------



